package cn.com.css.taxInspection.attention.controller;

import cn.com.css.taxInspection.attention.entity.AjcxCondition;
import cn.com.css.taxInspection.attention.entity.AjmxbEntity;
import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.attention.service.CaseAjxxRelationService;
import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.JcSjlEntity;
import cn.com.css.taxInspection.gt3.service.JcSjlService;
import cn.com.css.taxInspection.statistics.vo.JcjfReq;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * ${comments}
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-05-20 14:17:59
 */
@Controller
@RequestMapping("/")
public class CaseAjxxRelationController {
    @Resource
    private CaseAjxxRelationService caseAjxxRelationService;
    @Resource
    private JcSjlService jcSjlService;


    @ApiOperation(value = "新增")
    @PostMapping("caseAjxxRelation/add")
    @ResponseBody
    public DataResult add(@RequestBody CaseAjxxRelation caseAjxxRelation){
        caseAjxxRelationService.save(caseAjxxRelation);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("caseAjxxRelation/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        caseAjxxRelationService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("caseAjxxRelation/update")
    @ResponseBody
    public DataResult update(@RequestBody CaseAjxxRelation caseAjxxRelation){
        caseAjxxRelationService.updateById(caseAjxxRelation);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("caseAjxxRelation/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody AjcxCondition req){
        Page page = new Page(req.getPage(), req.getLimit());
        if(!StringUtils.isEmpty(req.getAjztDm())){
            req.setAjztDms(Arrays.asList(req.getAjztDm().split(",")));
        }
        LambdaQueryWrapper<CaseAjxxRelation> queryWrapper = Wrappers.lambdaQuery();
        String[] allJaCaseIds = {"1753235180390457346","4A83C7C7819843A6BC439E31E864FDDB","1753235137625333761","1753235212262973441",
                "1753235156424204289","CD7144F1D8EF4F2A807B56BB6EF836C3","1753235199331934210","1753235225022046209"};
        queryWrapper.in(CaseAjxxRelation::getCaseId, Arrays.asList(allJaCaseIds));
        if(!StringUtils.isEmpty(req.getNsrsbh())){
            queryWrapper.eq(CaseAjxxRelation::getNsrsbh, req.getNsrsbh());
        }
        if(!StringUtils.isEmpty(req.getNsrmc())){
            queryWrapper.eq(CaseAjxxRelation::getNsrmc, req.getNsrmc());
        }
        if(!StringUtils.isEmpty(req.getAjbh())){
            queryWrapper.eq(CaseAjxxRelation::getAjbh, req.getAjbh());
        }
        if(TsBqUtils.isNotNull(req.getAjztDms())){
            if(req.getAjztDm().indexOf("901")!= -1){
                queryWrapper.eq(CaseAjxxRelation::getAjztDm,"901");
            }
            for(int i =0;i<req.getAjztDms().size();i++){
                String tmpajztdm =  req.getAjztDms().get(i);
                if(req.getAjztDm().indexOf("901")!= -1){
                    if("901".equals(tmpajztdm)){
                        continue;
                    }
                    queryWrapper.or().likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                }else {
                    if(i == 0){
                        queryWrapper.likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                    }else {
                        queryWrapper.or().likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                    }
                }
            }
        }
        IPage<CaseAjxxRelation> iPage = caseAjxxRelationService.page(page, queryWrapper);
        List<CaseAjxxRelation> records = iPage.getRecords();
        for(CaseAjxxRelation ajr:records){
            String jcajxxuuid = ajr.getJcajxxuuid();
            JcSjlEntity jcSjlEntity = jcSjlService.getByJcajxxuuid(jcajxxuuid);
           if(TsBqUtils.isNotNull(jcSjlEntity)){
               Date larq = jcSjlEntity.getLarq();//立案日期
               Date jcjsrq = jcSjlEntity.getJcjsrq();//检查结束日期
               Date sljsrq = jcSjlEntity.getSljsrq();//审理结束日期
               Date sldjrq = jcSjlEntity.getSldjrq();//审理登记日期
               Date zxwbrq = jcSjlEntity.getZxwbrq();//执行完毕日期
               Date zxdjrq = jcSjlEntity.getZxdjrq();//执行登记日期
               Date jarq = jcSjlEntity.getJarq();//结案日期
               //检查环节耗时=检查结束日期（或当前日期）-立案日期
               //审理环节耗时=审理结束日期（或当前日期）-审理登记日期
               //执行环节耗时=执行完毕日期（或结案日期或当期日期）-执行登记日期
               //检查环节耗时
               String jchjhs = String.valueOf(TsBqUtils.calcMergeDays(
                       DateUtils.date2String(larq, DateUtils.DATE_DEFAULT_FORMAT),
                       DateUtils.date2String( TsBqUtils.isNull(jcjsrq)? new Date():jcjsrq, DateUtils.DATE_DEFAULT_FORMAT)));
               ajr.setJchjhs(jchjhs);
               //审理环节耗时
               String slhjhs = String.valueOf(TsBqUtils.calcMergeDays(
                       DateUtils.date2String(sldjrq, DateUtils.DATE_DEFAULT_FORMAT),
                       DateUtils.date2String( TsBqUtils.isNull(sljsrq)? new Date():sljsrq, DateUtils.DATE_DEFAULT_FORMAT)));
               ajr.setSlhjhs(slhjhs);
               //执行环节耗时
               String zxhjhs = String.valueOf(TsBqUtils.calcMergeDays(
                       DateUtils.date2String(zxdjrq, DateUtils.DATE_DEFAULT_FORMAT),
                       DateUtils.date2String( TsBqUtils.isNull(zxwbrq)?    TsBqUtils.isNull(jarq)? new Date():jarq  :zxwbrq, DateUtils.DATE_DEFAULT_FORMAT)));
               ajr.setZxhjhs(zxhjhs);
           }
        }
        iPage.setRecords(records);
        return DataResult.success(iPage);
    }
    @ApiOperation(value = "导出所有耗时数据")
    @PostMapping("caseAjxxRelation/caseAjxxRelationListForExport")
    @ResponseBody
    public DataResult caseAjxxRelationListForExport(@RequestBody AjcxCondition req){
        if(!StringUtils.isEmpty(req.getAjztDm())){
            req.setAjztDms(Arrays.asList(req.getAjztDm().split(",")));
        }
        LambdaQueryWrapper<CaseAjxxRelation> queryWrapper = Wrappers.lambdaQuery();
        String[] allJaCaseIds = {"1753235180390457346","4A83C7C7819843A6BC439E31E864FDDB","1753235137625333761","1753235212262973441",
                "1753235156424204289","CD7144F1D8EF4F2A807B56BB6EF836C3","1753235199331934210","1753235225022046209"};
        queryWrapper.in(CaseAjxxRelation::getCaseId, Arrays.asList(allJaCaseIds));
        if(!StringUtils.isEmpty(req.getNsrsbh())){
            queryWrapper.eq(CaseAjxxRelation::getNsrsbh, req.getNsrsbh());
        }
        if(!StringUtils.isEmpty(req.getNsrmc())){
            queryWrapper.eq(CaseAjxxRelation::getNsrmc, req.getNsrmc());
        }
        if(!StringUtils.isEmpty(req.getAjbh())){
            queryWrapper.eq(CaseAjxxRelation::getAjbh, req.getAjbh());
        }
        if(TsBqUtils.isNotNull(req.getAjztDms())){
            if(req.getAjztDm().indexOf("901")!= -1){
                queryWrapper.eq(CaseAjxxRelation::getAjztDm,"901");
            }
            for(int i =0;i<req.getAjztDms().size();i++){
                String tmpajztdm =  req.getAjztDms().get(i);
                if(req.getAjztDm().indexOf("901")!= -1){
                    if("901".equals(tmpajztdm)){
                        continue;
                    }
                    queryWrapper.or().likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                }else {
                    if(i == 0){
                        queryWrapper.likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                    }else {
                        queryWrapper.or().likeRight(CaseAjxxRelation::getAjztDm,tmpajztdm);
                    }
                }
            }
        }
        List<CaseAjxxRelation> records = caseAjxxRelationService.list(queryWrapper);
        for(CaseAjxxRelation ajr:records){
            String jcajxxuuid = ajr.getJcajxxuuid();
            JcSjlEntity jcSjlEntity = jcSjlService.getByJcajxxuuid(jcajxxuuid);
            ajr.setAjbh(ajr.getAjbh()+"\t");
            ajr.setNsrsbh(ajr.getNsrsbh()+"\t");
            if(TsBqUtils.isNotNull(jcSjlEntity)){
                Date larq = jcSjlEntity.getLarq();//立案日期
                Date jcjsrq = jcSjlEntity.getJcjsrq();//检查结束日期
                Date sljsrq = jcSjlEntity.getSljsrq();//审理结束日期
                Date sldjrq = jcSjlEntity.getSldjrq();//审理登记日期
                Date zxwbrq = jcSjlEntity.getZxwbrq();//执行完毕日期
                Date zxdjrq = jcSjlEntity.getZxdjrq();//执行登记日期
                Date jarq = jcSjlEntity.getJarq();//结案日期
                //检查环节耗时=检查结束日期（或当前日期）-立案日期
                //审理环节耗时=审理结束日期（或当前日期）-审理登记日期
                //执行环节耗时=执行完毕日期（或结案日期或当期日期）-执行登记日期
                //检查环节耗时
                String jchjhs = String.valueOf(TsBqUtils.calcMergeDays(
                        DateUtils.date2String(larq, DateUtils.DATE_DEFAULT_FORMAT),
                        DateUtils.date2String( TsBqUtils.isNull(jcjsrq)? new Date():jcjsrq, DateUtils.DATE_DEFAULT_FORMAT)));
                ajr.setJchjhs(jchjhs);
                //审理环节耗时
                String slhjhs = String.valueOf(TsBqUtils.calcMergeDays(
                        DateUtils.date2String(sldjrq, DateUtils.DATE_DEFAULT_FORMAT),
                        DateUtils.date2String( TsBqUtils.isNull(sljsrq)? new Date():sljsrq, DateUtils.DATE_DEFAULT_FORMAT)));
                ajr.setSlhjhs(slhjhs);
                //执行环节耗时
                String zxhjhs = String.valueOf(TsBqUtils.calcMergeDays(
                        DateUtils.date2String(zxdjrq, DateUtils.DATE_DEFAULT_FORMAT),
                        DateUtils.date2String( TsBqUtils.isNull(zxwbrq)?    TsBqUtils.isNull(jarq)? new Date():jarq  :zxwbrq, DateUtils.DATE_DEFAULT_FORMAT)));
                ajr.setZxhjhs(zxhjhs);
            }
        }
        return DataResult.success(records);
    }


    @PostMapping("caseAjxxRelation/ajmxbList")
    @ApiOperation(value = "案件明细表列表数据")
    @LogAnnotation(title = "案件明细表列表数据", action = "案件明细表列表数据")
    @ResponseBody
    public DataResult ajmxbList(@RequestBody AjmxbEntity req) {
        Page page = new Page();
        List<AjmxbEntity> ajmxbEntities = caseAjxxRelationService.ajmxbList(req);
        page.setRecords(caseAjxxRelationService.handlerAjmxbList(ajmxbEntities));
        page.setTotal(caseAjxxRelationService.ajmxbListTotal());
        page.setCurrent(req.getPage());
        return DataResult.success(page);
    }
}
